Here, the first thing to do is to retrieve the data processed at the end of vignette("data_exploration_2018").
# read the processed data
data_2018_filter <- readRDS("tmp/data_2018_filter.rds")
# This piece of code is only there to show how to draw a polylines with a
# gradient color using leaflet. We're not using it due to the size of the
# created map, and will continue using circle marker
# datasets used to display map
df_driftrate <- unique(data_2018_filter[
.id == "ind_2018070" &
divetype == "2: drift",
.(.id, lat, lon, dduration)
])
# color palette
pal <- colorNumeric(
palette = "YlGnBu",
domain = df_driftrate$dduration
)
# add
df_driftrate[, `:=`(
nextLat = shift(lat),
nextLon = shift(lon),
color = pal(df_driftrate$dduration)
)]
# interactive map
gradient_map <- leaflet() %>%
setView(lng = -122, lat = 38, zoom = 2) %>%
addTiles()
# add lines
for (i in 1:nrow(df_driftrate)) {
gradient_map <- addPolylines(
map = gradient_map,
data = df_driftrate,
lat = as.numeric(df_driftrate[i, c("lat", "nextLat")]),
lng = as.numeric(df_driftrate[i, c("lon", "nextLon")]),
color = df_driftrate[i, color],
weight = 3,
group = "individual_1"
)
}
# add layer control
gradient_map <- addLayersControl(
map = gradient_map,
overlayGroups = c("individual_1"),
options = layersControlOptions(collapsed = FALSE)
)
Because for some data the contrast in changes was not enough marked, the only treatment applied on these data was to remove outliers for each variable using the interquartile range rule.
# interactive map
gradient_map <- leaflet() %>%
setView(lng = -132, lat = 48, zoom = 4) %>%
addTiles()
# loop by individuals and variable
grps <- NULL
for (i in seq(data_2018_filter[!is.na(lat), unique(.id)])) {
for (k in c("dduration", "maxdepth", "efficiency", "driftrate")) {
if (k == "driftrate") {
# set dataset used to plot
dataPlot <- unique(data_2018_filter %>%
.[order(date), ] %>%
.[.id == data_2018_filter[!is.na(lat), unique(.id)][i] &
divetype == "2: drift" &
!is.na(get(k)),
c("lat", "lon", k),
with = FALSE
] %>%
.[!is_outlier(get(k)), ])
# color palette creation
colPal <- colorNumeric(
palette = "BrBG",
domain = seq(
-dataPlot[, max(abs(driftrate))],
dataPlot[, max(abs(driftrate))],
0.1
)
)
} else {
# set dataset used to plot
dataPlot <- unique(data_2018_filter %>%
.[order(date), ] %>%
.[.id == data_2018_filter[!is.na(lat), unique(.id)][i] &
divetype != "2: drift" &
!is.na(get(k)),
c("lat", "lon", k),
with = FALSE
] %>%
.[!is_outlier(get(k)), ])
# color palette creation
colPal <- colorNumeric(
palette = "YlGnBu",
domain = dataPlot[, get(k)]
)
}
# add color to dataset
dataPlot[, color := colPal(dataPlot[, get(k)])]
# add size column
dataPlot[, radius := 3]
# mark the beginning of the trip
dataPlot[1, `:=`(
color = "green",
radius = 4
)]
# mark the end of the trip
dataPlot[.N, `:=`(
color = "red",
radius = 4
)]
# reorder to make the end and the beginning in front
dataPlot <- rbind(dataPlot[-1, ], dataPlot[1, ])
# convert to sf
dataPlot <- sf::st_as_sf(dataPlot, coords = c("lon", "lat"), crs = 4326)
# add markers to map
gradient_map <- addGlPoints(
map = gradient_map,
data = dataPlot,
radius = dataPlot$radius * 4,
stroke = FALSE,
fillColor = ~color,
group = paste(data_2018_filter[, unique(.id)][i], "-", k)
) %>%
addLegend("bottomleft",
data = dataPlot,
group = paste(data_2018_filter[, unique(.id)][i], "-", k),
pal = colPal,
values = ~ get(k),
title = k
)
# retrieve groups
grps <- c(grps, paste(data_2018_filter[, unique(.id)][i], "-", k))
}
}
# add layer control
gradient_map <- addLayersControl(
map = gradient_map,
overlayGroups = grps,
options = layersControlOptions(collapsed = TRUE)
) %>% hideGroup(grps)
# display
gradient_map